<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of TeamController
 *
 * @author Admin
 */
class Marketing_TargetController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function preDispatch() {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect('/user/login');
        }

        $view = new Zend_View();
        $view->headTitle(' > Marketing > Target');
        $this->view->userDetail = $auth->getIdentity();
    }

    public function indexAction() {
        $switchUserDetailModel = new Application_Model_SwitchUserDetailModel();
        $paginator = $switchUserDetailModel->getAllUserTargets($this->_request);
        $this->view->userId = $this->_request->getParam('user');
        $this->view->switchId = $this->_request->getParam('switch');
        $this->view->paginator = $paginator;

        $userModel = new Application_Model_UserModel();
        $userDetail = $userModel->getUser('id', $this->_request->getParam('user'));
        $this->view->userDetails = $userDetail;
    }

    public function listAction() {
        $this->_helper->layout->disableLayout();
        $switchUserDetailModel = new Application_Model_SwitchUserDetailModel();
        $paginator = $switchUserDetailModel->getAllUserTargets($this->_request);
        $this->view->userId = $this->_request->getParam('user');
        $this->view->switchId = $this->_request->getParam('switch');
        $this->view->paginator = $paginator;

        $userModel = new Application_Model_UserModel();
        $userDetail = $userModel->getUser('id', $this->_request->getParam('user'));
        $this->view->userDetails = $userDetail;
    }

    public function addAction() {
        $this->_helper->layout->disableLayout();
        $this->view->userId = $this->_request->getParam('user');
        $this->view->switchId = $this->_request->getParam('switch');

        $userModel = new Application_Model_UserModel();
        $userDetail = $userModel->getUser('id', $this->_request->getParam('user'));
        $this->view->userDetails = $userDetail;
        $switchModel = new Application_Model_SwitchModel();
        $switchDetail = $switchModel->getSwitch('id', $this->_request->getParam('switch'));
        $this->view->switchDetail = $switchDetail;

        $form = new Application_Form_target_AddForm();
        // if request is post
        if ($this->_request->isPost()) {
            // if form is valid
            if ($form->isValid($this->_request->getPost())) {
                $data = array(
                    'user_id' => $this->_request->getParam('user'),
                    'switch_id' => $this->_request->getParam('switch'),
                    'month' => $this->_request->getPost('monthList'),
                    'year' => $this->_request->getPost('yearList'),
                    'monthlytarget' => $this->_request->getPost('houseTarget'),
                    'monthly_max' => $this->_request->getPost('houseMax'),
                    'commission_percent' => $this->_request->getPost('commissionPercent'),
                    'commission_percent_max' => $this->_request->getPost('commissionPercentMax'),
                );
                $switchUserDetailModel = new Application_Model_SwitchUserDetailModel();
                $switchUserDetailModel->save($data);
                //   $this->_redirect("planning/cabinet");
                //  exit;
            }
        }
        // assign form variable to view
        $this->view->form = $form;
    }

    public function editAction() {
        $this->_helper->layout->disableLayout();
        $this->view->id = $this->_request->getParam('id');
        $this->view->userId = $this->_request->getParam('user');
        $this->view->switchId = $this->_request->getParam('switch');

        $userModel = new Application_Model_UserModel();
        $userDetail = $userModel->getUser('id', $this->_request->getParam('user'));
        $this->view->userDetails = $userDetail;
        $switchModel = new Application_Model_SwitchModel();
        $switchDetail = $switchModel->getSwitch('id', $this->_request->getParam('switch'));
        $this->view->switchDetail = $switchDetail;

        $switchUserDetailModel = new Application_Model_SwitchUserDetailModel();
        $switchUserDetail = $switchUserDetailModel->getDetail('id',$this->_request->getParam('id'));
        $form = new Application_Form_target_AddForm();
        $form->populate(array(
            'houseTarget' => $switchUserDetail['monthlytarget'],
            'houseMax' => $switchUserDetail['monthly_max'],
            'commissionPercent' => $switchUserDetail['commission_percent'],
            'commissionPercentMax' => $switchUserDetail['commission_percent_max']
        ));
        // if request is post
        if ($this->_request->isPost()) {
            // if form is valid
            if ($form->isValid($this->_request->getPost())) {
                $data = array(
                    'id'=>$this->_request->getParam('id'),
                    'user_id' => $this->_request->getParam('user'),
                    'switch_id' => $this->_request->getParam('switch'),
                    'month' => $this->_request->getPost('monthList'),
                    'year' => $this->_request->getPost('yearList'),
                    'monthlytarget' => $this->_request->getPost('houseTarget'),
                    'monthly_max' => $this->_request->getPost('houseMax'),
                    'commission_percent' => $this->_request->getPost('commissionPercent'),
                    'commission_percent_max' => $this->_request->getPost('commissionPercentMax'),
                );
                $switchUserDetailModel = new Application_Model_SwitchUserDetailModel();
                $switchUserDetailModel->save($data);
                //   $this->_redirect("planning/cabinet");
                //  exit;
            }
        }
        // assign form variable to view
        $this->view->form = $form;
    }

}